package io.renren.modules.jsf.service.impl;

import io.renren.modules.sys.dao.SysUserDao;
import io.renren.modules.sys.dao.SysUserRoleDao;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.entity.SysUserRoleEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.jsf.dao.YuyueDao;
import io.renren.modules.jsf.entity.YuyueEntity;
import io.renren.modules.jsf.service.YuyueService;

import static io.renren.common.utils.ShiroUtils.getUserId;


@Service("yuyueService")
public class YuyueServiceImpl extends ServiceImpl<YuyueDao, YuyueEntity> implements YuyueService {

    @Autowired
    SysUserRoleDao userRoleDao;
    @Autowired
    SysUserDao userDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Long userId = getUserId();

        IPage<YuyueEntity> page = null;
        SysUserEntity userEntity = userDao.selectById(userId);
        SysUserRoleEntity roleEntity = userRoleDao.selectById(userId);
        if (roleEntity == null) {
            page = this.page(
                    new Query<YuyueEntity>().getPage(params),
                    new QueryWrapper<YuyueEntity>()
            );
        } else {
            if (roleEntity.getRoleId() > 1) {
                page = this.page(
                        new Query<YuyueEntity>().getPage(params),
                        new QueryWrapper<YuyueEntity>().eq("username", userEntity.getUsername())
                );
            } else {
                page = this.page(
                        new Query<YuyueEntity>().getPage(params),
                        new QueryWrapper<YuyueEntity>().eq("teachername", userEntity.getUsername())
                );
            }
        }
        return new PageUtils(page);
    }

}
